/**
 * Copyright(c) 2010 Ismole Co.Ltd.
 * All Right Reserved.
 */
package
{
	/**
	 * <p>项目名称: Ninja_New</p>
	 * <p>所属模块: 描述这个类属于哪一系统哪一模块</p>
	 * <p>实现功能: 描述该类实现了什么功能，尽可能详尽</p>
	 * <p>当前状态: 未完成</p>
	 * @author 杨邦庆
	 * @version 1.0
	 * @param arg[0]  arg[1] ...
	 */
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	import flash.system.ApplicationDomain;
	
	public class AssetLoader extends EventDispatcher
	{
		/**
		 * 素材的导出名称
		 */		
		private var _linkURL:String;
		
		/**
		 * 素材文件的所在路径
		 */		
		private var _url:String;
		/**
		 * 素材加载的对象
		 */		
		private var _load:LoaderResourceItem;
		
		/**
		 * 回调函数
		 */		
		private var _callback:Function;
		
		private var _loadType:int = 0;
		/**
		 * 构造器
		 * @param link 素材的导出名称
		 * @param url 素材文件的所在路径(相对)d
		 * @param callback 加载素材完成后的回调方法
		 * @param param 素材加载的配置参数
		 * @param param LoadPriorityType.IMMEDIATELY
		 * 
		 */	
		public function AssetLoader(link:String, url:String, 
									callback:Function = null, 
									loadType:int = 2)
		{
			_linkURL = link;
			_url = url;
			_callback = callback;
			_loadType = loadType;
		}
		
		private function loadItem():void
		{
			_load = new LoaderResourceItem(_url);
			_load.addEventListener(Event.COMPLETE, onComplete);
			_load.addEventListener("loadError", onError);
			_load.load();
		}
		
		private function onError(e:Event):void
		{
//			LoadMaterialQueue.getInstance().removeItem(this._url);
		}
		
		private function onComplete(evt:Event):void
		{
			finish();
		}
		
		public function load():void
		{
			if (!ApplicationDomain.currentDomain.hasDefinition(_linkURL))
			{
				loadItem();
			}
			else
			{
				finish();
			}
		}
		
		public function finish():void
		{
			if (_callback != null)
			{ 
				_callback(_linkURL, _url);
			}
		}
	}
}